package com.ujson.uaa.persistence.impl;

import com.ujson.base.persistence.impl.AbstractDaoImpl;
import com.ujson.uaa.persistence.UserDao;
import com.ujson.uaa.persistence.UserDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaContext;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl extends AbstractDaoImpl<UserDo, Long> implements UserDao {

    @Autowired
    public UserDaoImpl(JpaContext context) {
        super(context, UserDo.class);
    }

    public UserDo findByUserName(String username) {
        List result = getEm().createNativeQuery("select * from t_user where name=:username limit 1", UserDo.class)
                .setParameter("username", username)
                .getResultList();
        if (result.size() > 0) {
            return (UserDo) result.get(0);
        }
        return null;
    }

    @Override
    public UserDo save(UserDo userDo) {
        return super.save(userDo);
    }
}
